package lowijs.XfireClient.packet;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import lowijs.XfDroid.FriendDBAdapter;
import lowijs.XfireClient.util.Util;

public abstract class Packet {
	private final ByteBuffer buf = ByteBuffer.allocate(8192);
	
	protected String gamename;
	protected byte[] image;
	
	public Packet() {
		buf.order(ByteOrder.LITTLE_ENDIAN);
		buf.position(2);
	}

	/**
	 * Puts a byte in the byte buffer.
	 * @param b the byte to put.
	 */
	protected void putByte(byte b) {
		buf.put(b);
	}

	/**
	 * Puts a byte[] in the byte buffer.
	 * @param bs the byte[] to put.
	 */
	protected void putBytes(byte[] bs) {
		buf.put(bs);
	}

	/**
	 * Puts a byte[] in the byte buffer.
	 * @param bs the byte[] to put.
	 */
	protected void putInt(int value) {
		buf.putInt(value);
	}
	
	/**
	 * Sets the size of the packet in the first two bytes.
	 */
	protected void setSize() {
		int size = buf.position();
		buf.putShort(0, (short)size);
		buf.position(size);
	}

	/**
	 * Converts two bytes (low and high byte) into an int.
	 * @param low the low byte.
	 * @param high the high byte.
	 * @return the int created from the low and high byte.
	 */
	protected static int fromLowHigh(byte low, byte high) {
		return 0x00 | (low & 0xFF) | ((high & 0xFF) << 8);
	}

	/**
	 * Puts a string with length into the byte buffer.
	 * @param data the string to put.
	 */
	protected void putString(String data) {
		int length = data.length();
		
		if(length > 0xFF)
			putByte((byte)0x02);
		else
			putByte((byte)0x01);

		buf.putShort( (short)length );
		putBytes(data.getBytes());
	}
	
	protected void putShortString(String data) {
		buf.putShort( (short)data.length() );
		putBytes(data.getBytes());
	}
	
	protected static boolean isZero(byte[] bytes) {
		for (int i = 0; i < bytes.length; i++) {
			if ((bytes[i] & 0xFF) > 0)
				return false;
		}
		return true;
	}
	
	/**
	 * Returns the packet as an array of bytes.
	 * @return the packet as a byte[].
	 */
	public byte[] getBytes() {
		setSize();
		
		buf.flip();
		int size = buf.limit();
		
		byte[] bytes = new byte[size];
		buf.get(bytes, 0, size);
		
		return bytes;
	}
	
	public  static String getGamedataById(int gameid) {
		String image = null;

		try {
			File file = Util.getFile("/" + gameid + ".dat");

			if (file.exists()) {
				file.delete();
			}

			File oldimg = Util.getFile("/games/" + gameid + ".gif");
			file = Util.getFile("/games/" + gameid + ".xfd");

			if (oldimg.exists()) {
				oldimg.renameTo(file);
			}

			if (!file.exists()) {
				file.getParentFile().mkdirs();

				URL url = new URL("http://domnious.com/michiel/gamedata.php?game=" + gameid);
				DataInputStream in = new DataInputStream(url.openStream());

				int nameLen = in.read(), imgLow = in.read(), imgHigh = in.read();
				int imgLen = 0 | imgLow | (imgHigh << 8);

				byte[] buffer = new byte[nameLen];
				in.read(buffer, 0, nameLen);
				String str = new String(buffer);
				FriendDBAdapter db = FriendDBAdapter.getInstance(null);
				db.gamename(gameid, str);

				buffer = new byte[imgLen];
				in.read(buffer, 0, imgLen);
				System.out.println(str + ": " + new String(buffer, 0, 5));

				in.close();

				FileOutputStream fos = new FileOutputStream(file);
				fos.write(buffer);
				fos.close();
			}

			image = file.getAbsolutePath();
			System.out.println(image);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

		return image;
	}
}
